package com.iemes.controller.pretest;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.PretesCageFormMap;
import com.iemes.entity.PretesCageLineFormMap;
import com.iemes.entity.PretesExcessStockCheckFormMap;
import com.iemes.entity.PretestDentsityFormMap;
import com.iemes.entity.PretestHooperFormMap;
import com.iemes.mapper.pretest.PreStockCheckMapper;
import com.iemes.mapper.pretest.PretesCageLineMapper;
import com.iemes.mapper.pretest.PretesCageMapper;
import com.iemes.mapper.pretest.PretesDentsityMapper;
import com.iemes.mapper.pretest.PretesHooperMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


//余料盘点
@RequestMapping("/prestock")
@Controller
public class PreStockCheckController extends BaseController {

//  密度
	@Inject
	private PretesDentsityMapper pretestDentsityMapper;
//  斗
	@Inject
	private PretesHooperMapper pretesHooperMapper;
//  笼 编号
	@Inject
	private PretesCageMapper pretesCageMapper;

//笼区域
	@Inject
	private PretesCageLineMapper pretesCageLineMapper;

	
	//列表

	@Inject
	private PreStockCheckMapper prestockcheckMapper;
	
	
	
	@RequestMapping("stockcheck")
	public String stockcheck(Model model){
		model.addAttribute("res", findByRes());
		model.addAttribute("dentsity", pretestDentsityMapper.findByWhere(getFormMap(PretestDentsityFormMap.class)));
		model.addAttribute("hooper", pretesHooperMapper.findByWhere(getFormMap(PretestHooperFormMap.class)));
		return Common.BACKGROUND_PATH + "/pretest/prestock/prestock_list";
	}
	
	@ResponseBody
	@RequestMapping("prestockcheck_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception
	{
		PretesExcessStockCheckFormMap stockcheckFormMap = getFormMap(PretesExcessStockCheckFormMap.class);
		List<Map<String, Object>> pretest = prestockcheckMapper.getAll(stockcheckFormMap);
		
		LuogicPaging(pageNow, pageSize, pretest);
		pageView.setRecords(pretest);
		return pageView;
	}
	
//新增余料盘点并清点剩余量
	
	@RequestMapping("stockcheck_add")
	public String addUI(Model model) throws Exception {
		model.addAttribute("cage", pretesCageMapper.findByWhere(getFormMap(PretesCageFormMap.class)));
		model.addAttribute("caline", pretesCageLineMapper.findByWhere(getFormMap(PretesCageLineFormMap.class)));
		return Common.BACKGROUND_PATH + "/pretest/prestock/prestock_add";
	}
	
	
	@ResponseBody
	@RequestMapping("prestockcheck_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="余料盘点统计表",methods="余料盘点统计表-余料盘点统计表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(HttpServletRequest request) throws Exception {
		PretesExcessStockCheckFormMap stockcheckFormMap = getFormMap(PretesExcessStockCheckFormMap.class);
		
		try {
					
					/*插入当前时间*/
					SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm:ss");
					Date date=new Date();
					
					
					
					//获取输入的剩料量%
					String remain=request.getParameter("surplusfeedratekg");
					//把输入的字符串变成小数
					double r=Double.parseDouble(remain);
					System.out.println(r);
			
					
					//拿到满笼(笼存料量)
					String id = getPara("PretesExcessStockCheckFormMap.cage_id");
					if(Common.isNotEmpty(id))
					{
						System.out.println(id);
						
						PretesCageFormMap cagemaps= baseMapper.findbyFrist("id", id, PretesCageFormMap.class);
						String cage= cagemaps.get("storesquantity").toString();
						double c=Double.parseDouble(cage);
						
						System.out.println(c);
						
						//满笼*剩料量=剩余量
						double surplus=c* r;
						
				
						stockcheckFormMap.put("createtime", sdf1.format(date));
						stockcheckFormMap.put("time", sdf2.format(date));
						stockcheckFormMap.put("surpluskg",surplus);
						
						prestockcheckMapper.addEntity(stockcheckFormMap);
					}	
				} catch (Exception e) {
					e.printStackTrace();
					
				}
				
		return "success";
	}
	
}











